<?php

/**
 * 商城设置
 * @author renjianhong
 * @date 2021-8-03 16:47
 */
namespace Modules\Mallsetting\Http\Controllers\Admin;

use Illuminate\Http\Request;
use Modules\Mallsetting\Http\Controllers\Controller;
use Modules\Mallsetting\Entities\MallSetting;
use Modules\Mallsetting\Entities\SmsSetting;
use Modules\Mallsetting\Entities\MailSetting;
use Modules\Mallsetting\Entities\Area;
use Modules\Mallsetting\Entities\OfferPrice;
use Modules\Mallsetting\Entities\TerritorialLimitation;

class MallSettingController extends Controller
{
    
    /**
     * 基础配置
     */
    public function setting(Request $request , MallSetting $MallSettingModel)
    {
        if($request->isMethod('post')) {
            $data = $request->post();
            if(array_key_exists('file',$data)){ //检查数组中此键名是否存在
                unset($data['file']);//删除键名及其值
            }
            
            if(!isset($data['payment'])){
                $data['payment'] = ['wechat' => 'on'];
                $data['payment'] = json_encode($data['payment']);
            }else{
                $data['payment'] = json_encode($data['payment']);
            }
            
            
            if(!$data['id']){
                $data['created_at'] = date('Y-m-d H:i:s');
                $reulst = $MallSettingModel->insertGetId($data);
            }else{
                $reulst = $MallSettingModel->where('id',$data['id'])->update($data);
            }
            if($reulst){
                return $this->success("操作成功");
            }else{
                return $this->failed("操作失败");
            }
            
        }
        $info = $MallSettingModel->first();
        if($info){
            //客服图标
            $service_pic = $MallSettingModel->getIsDomain($info->service_pic);
            if($service_pic){
                $info->service_pic_show = $info->service_pic;
            }else{
                $info->service_pic_show = $MallSettingModel->getShowImage($info->service_pic);
            }
            //拨号图标
            $dial_pic = $MallSettingModel->getIsDomain($info->dial_pic);
            if($dial_pic){
                $info->dial_pic_show = $info->dial_pic;
            }else{
                $info->dial_pic_show = $MallSettingModel->getShowImage($info->dial_pic);
            }
            //客服外链图标
            $web_service = $MallSettingModel->getIsDomain($info->web_service);
            if($web_service){
                $info->web_service_show = $info->web_service;
            }else{
                $info->web_service_show = $MallSettingModel->getShowImage($info->web_service);
            }
            //悬浮图标
            $wxapp_pic_url = $MallSettingModel->getIsDomain($info->wxapp_pic_url);
            if($wxapp_pic_url){
                $info->wxapp_pic_url_show = $info->wxapp_pic_url;
            }else{
                $info->wxapp_pic_url_show = $MallSettingModel->getShowImage($info->wxapp_pic_url);
            }
            $info->payment = json_decode($info->payment,true);
        }
        
        return view('mallsetting::admin.mall_setting.setting',compact('info','MallSettingModel'));
    }

    /**
     * 短信通知配置
     */
    public function sms_setting(Request $request , SmsSetting $SmsSettingModel)
    {
        if($request->isMethod('post')) {
            $data = $request->post();
            $data['tpl_order'] = json_encode($data['tpl_order']);
            $data['tpl_refund'] = json_encode($data['tpl_refund']);
            $data['tpl_code'] = json_encode($data['tpl_code']);
            if(!$data['id']){
                $data['created_at'] = date('Y-m-d H:i:s');
                $reulst = $SmsSettingModel->insertGetId($data);
            }else{
                $reulst = $SmsSettingModel->where('id',$data['id'])->update($data);
            }
            if($reulst){
                return $this->success("操作成功");
            }else{
                return $this->failed("操作失败");
            }
        }
        $info = $SmsSettingModel->first();
        if($info){
            $info->tpl_order = json_decode($info->tpl_order,true);
            $info->tpl_refund = json_decode($info->tpl_refund,true);
            $info->tpl_code = json_decode($info->tpl_code,true);
        }
        return view('mallsetting::admin.mall_setting.sms_setting',compact('info','SmsSettingModel'));
    }

    /**
     * 邮件通知配置
     */
    public function mail_setting(Request $request , MailSetting $MailSettingModel)
    {
        if($request->isMethod('post')) {
            $data = $request->post();
            
            if(!$data['id']){
                $data['created_at'] = date('Y-m-d H:i:s');
                $reulst = $MailSettingModel->insertGetId($data);
            }else{
                $reulst = $MailSettingModel->where('id',$data['id'])->update($data);
            }
            if($reulst){
                return $this->success("操作成功");
            }else{
                return $this->failed("操作失败");
            }
        }
        $info = $MailSettingModel->first();
        
        return view('mallsetting::admin.mall_setting.mail_setting',compact('info','MailSettingModel'));
    }

    /**
     * 区域限制购买规则
     */
    public function territorial_limitation(Request $request , TerritorialLimitation $TerritorialLimitationModel)
    {
        if($request->isMethod('post')){
            $data = $request->input();
            
            if(isset($data['detail'])){
                $detail = array_values($data['detail']);
                $data['detail'] = json_encode($detail);
            }else{
                return $this->failed("请增加条目");
            }
            
            if(!$data['id']){
                $data['created_at'] = date('Y-m-d H:i:s');
                $reulst = $TerritorialLimitationModel->insertGetId($data);
            }else{
                $reulst = $TerritorialLimitationModel->where('id',$data['id'])->update($data);
            }
            if($reulst){
                return $this->success("操作成功");
            }else{
                return $this->failed("操作失败");
            }
        }
        $key_val = 0;
        $info = $TerritorialLimitationModel->first();
        if($info){
            $info->detail = array_values(json_decode($info->detail,true));//索引从0开始排序
            $list = $info->detail;
            $numItems = count($list);
            $i = 0;
            foreach($list as $key => $v){
                if(++$i === $numItems) {
                    $key_val = $numItems;
                }
            }
        }
        
        return view('mallsetting::admin.mall_setting.territorial_limitation',compact('info','key_val','TerritorialLimitationModel'));
    }

    /**
     * 获取区域
     */
    public function getArea(Area $AreaModel)
    {
        //获取所有区域
        $province_list = $AreaModel->selectRaw("id,parent_id,name as title")->whereIn('level',['province','city','district'])->get()->toArray();
        $list = $this->buildMenuChild(1, $province_list);
        
        return $this->success(compact('list'));
    }

    /**
     * 递归获取子菜单
     */
    private function buildMenuChild($id, $province_list,$lst=0){
        
        $treeList = [];
        foreach ($province_list as $key => $v) {
            if ($id == $v['parent_id']) {
                $node = (array)$v;
                unset($province_list[$key]);

                $child = $this->buildMenuChild($v['id'], $province_list,2);
                if($child){
                    $node['children'] = $child;
                    $node['spread'] = false;//初始化节点是否展开
                }
                // todo 后续此处加上用户的权限判断
                $treeList[] = $node;
                
            }
        }
        
        return $treeList;
    }

    /**
     * 起送规则
     */
    public function offer_price(Request $request , OfferPrice $OfferPriceModel)
    {
        if($request->isMethod('post')){
            $data = $request->input();
            if(isset($data['detail'])){
                $detail = array_values($data['detail']);
                $data['detail'] = json_encode($detail);
            }else{
                return $this->failed("请增加条目");
            }
            
            if(!$data['id']){
                $data['created_at'] = date('Y-m-d H:i:s');
                $reulst = $OfferPriceModel->insertGetId($data);
            }else{
                $reulst = $OfferPriceModel->where('id',$data['id'])->update($data);
            }
            if($reulst){
                return $this->success("操作成功");
            }else{
                return $this->failed("操作失败");
            }
        }
        $key_val = 0;
        $info = $OfferPriceModel->first();
        if($info){
            $info->detail = array_values(json_decode($info->detail,true));//索引从0开始排序
            $list = $info->detail;
            $numItems = count($list);
            $i = 0;
            foreach($list as $key => $v){
                if(++$i === $numItems) {
                    $key_val = $numItems;
                }
            }
        }
        
        return view('mallsetting::admin.mall_setting.offer_price',compact('info','key_val','OfferPriceModel'));
    }
    
}
